package com.ugoodtech.mdcc.web.dto;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ugoodtech.mdcc.core.StatusConstants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.utils.StatusUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Copyright © 2013 All Rights Reserved, Ugood Technology, Inc.
 */
@ApiModel("外勤人员/管理人员")
public class UserProfileResponse {
    @ApiModelProperty("编号")
    private Long userId;
    @ApiModelProperty("姓名")
    private String name;
    @ApiModelProperty("手机号")
    private String phoneNum;
    //    @ApiModelProperty("类型")
//    private String type;
    @ApiModelProperty("所属子部门")
    private String department;
    @ApiModelProperty("状态")
    private String status;
    @ApiModelProperty("创建时间")
    private String creationTime;
    @ApiModelProperty("管理员姓名")
    private String managerUserName;
    @ApiModelProperty("职务")
    private String position;
    @ApiModelProperty("编号")
    private String serialNumber;
    //    @ApiModelProperty("角色")
//    private Long role;
    @ApiModelProperty("所属部门IDSs")
    private List<LabelValue> departmentIds;
    @ApiModelProperty("负责的省份产品")
    private String provinceAndBrand;
    @ApiModelProperty("中文显示用户类型")
    private String userTypeStr;
    //    @ApiModelProperty("关联的销售")
//    private LabelValue saleUser;
//    @ApiModelProperty("关联的技术部门")
//    private List<LabelValue> technologyDepartment;
//    @ApiModelProperty("关联的技术人员")
//    private LabelValue technologyUser;
    @ApiModelProperty("绑定的维修站")
    private List<LabelValue> stations;
    @ApiModelProperty("权限ID")
    private List<Long> authoritiesList;
    @ApiModelProperty("省份")
    private List<String> provinces;
    @ApiModelProperty("汽车品牌")
    private List<LabelValue> brands;
    @ApiModelProperty("是否为管理员")
    private Boolean isAdmin;

    @JsonIgnore
    private List<LabelValue> comboData = new ArrayList<>();

    public UserProfileResponse(UserProfile outsideUser) {
        if (outsideUser.getStationList() != null && outsideUser.getStationList().size() > 0) {
            List<LabelValue> comboData = new ArrayList<>();
            for (Station station : outsideUser.getStationList()) {
                LabelValue labelValue = new LabelValue(station.getName(), station.getId() + "");
                comboData.add(labelValue);
            }
            this.stations = comboData;
        }
        this.userId = outsideUser.getId();
        this.name = outsideUser.getRealName();
        if (null != outsideUser.getUser()) {
            this.phoneNum = outsideUser.getUser().getUsername();
            if (null != outsideUser.getUser().getUserType()) {
                String userType = outsideUser.getUser().getUserType().name();
                if (userType.equals("dealers")) {
                    this.userTypeStr = "经销商";
                } else {
                    this.userTypeStr = "内部人员";
                }
            }
            this.status = StatusUtil.getStatusStr(outsideUser.getUser().isEnabled(), StatusConstants.S_ENABLED, StatusConstants.S_DISABLED);
        }
        this.serialNumber = outsideUser.getSerialNumber() == null ? "" : outsideUser.getSerialNumber();
        if (null != outsideUser.getAddUser()) {
            this.managerUserName = outsideUser.getAddUser().getRealName();
        } else {
            this.managerUserName = "";
        }

        StringBuffer stringBuffer = new StringBuffer("");
        List<Department> departmentList = outsideUser.getDepartmentList();
        List<LabelValue> comboData = new ArrayList<>();
        for (Department department : departmentList) {
            LabelValue labelValue = new LabelValue(department.getTotalName(), department.getId() + "");
            comboData.add(labelValue);
            stringBuffer.append(department.getTotalName() + ",");
        }
        this.departmentIds = comboData;
        this.department = stringBuffer.toString();

        if (outsideUser.getDepartment() != null) {
            this.department = outsideUser.getDepartment().getTotalName() == null ? "" : outsideUser.getDepartment().getTotalName();
        }
        if (StringUtils.isNotEmpty(outsideUser.getProincesAndBrands())) {
            this.provinceAndBrand = outsideUser.getProincesAndBrands();
        } else {
            this.provinceAndBrand = "";
        }
        this.creationTime = outsideUser.getCreationTimeStr();

        Set<ModuleAuthority> moduleAuthorityList = outsideUser.getUser().getAuthorities();
        List<Long> ids = new ArrayList<>();
        for (ModuleAuthority moduleAuthority : moduleAuthorityList) {
            ids.add(moduleAuthority.getId());
        }
        this.authoritiesList = ids;


        List<Nation> nationList = outsideUser.getNationList();
        List<String> provincesList = new ArrayList<>();
        for (Nation nation : nationList) {
            provincesList.add(nation.getProvince());
        }
        this.provinces = provincesList;

        if (outsideUser.getBrandList() != null) {
            comboData = new ArrayList<>();
            for (Brand brand : outsideUser.getBrandList()) {
                LabelValue labelValue = new LabelValue(brand.getBrandName(), brand.getId() + "");
                comboData.add(labelValue);
            }
            this.brands = comboData;
        }

        if (StringUtils.isNotEmpty(outsideUser.getPosition())) {
            this.position = outsideUser.getPosition();
        } else {
            this.position = "";
        }
        this.isAdmin = outsideUser.isAdmin();


    }


    private List<LabelValue> getDepartmentIds(Department department) {
        LabelValue labelValue = new LabelValue(department.getTotalName(), department.getId() + "");
        comboData.add(labelValue);
        Department parentDepartment = department.getParent();
        if (parentDepartment != null) {
            getDepartmentIds(parentDepartment);
        }
        return comboData;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

//    public String getType() {
//        return type;
//    }
//
//    public void setType(String type) {
//        this.type = type;
//    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(String creationTime) {
        this.creationTime = creationTime;
    }

    public String getManagerUserName() {
        return managerUserName;
    }

    public void setManagerUserName(String managerUserName) {
        this.managerUserName = managerUserName;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

//    public Long getRole() {
//        return role;
//    }
//
//    public void setRole(Long role) {
//        this.role = role;
//    }


    public String getProvinceAndBrand() {
        return provinceAndBrand;
    }

    public void setProvinceAndBrand(String provinceAndBrand) {
        this.provinceAndBrand = provinceAndBrand;
    }

    public List<LabelValue> getDepartmentIds() {
        return departmentIds;
    }

    public void setDepartmentIds(List<LabelValue> departmentIds) {
        this.departmentIds = departmentIds;
    }

    public List<LabelValue> getComboData() {
        return comboData;
    }

    public void setComboData(List<LabelValue> comboData) {
        this.comboData = comboData;
    }

    public String getUserTypeStr() {
        return userTypeStr;
    }

    public void setUserTypeStr(String userTypeStr) {
        this.userTypeStr = userTypeStr;
    }


//    public LabelValue getSaleUser() {
//        return saleUser;
//    }
//
//    public void setSaleUser(LabelValue saleUser) {
//        this.saleUser = saleUser;
//    }
//
//    public LabelValue getTechnologyUser() {
//        return technologyUser;
//    }
//
//    public void setTechnologyUser(LabelValue technologyUser) {
//        this.technologyUser = technologyUser;
//    }

    public List<LabelValue> getStations() {
        return stations;
    }

    public void setStations(List<LabelValue> stations) {
        this.stations = stations;
    }

    public List<Long> getAuthoritiesList() {
        return authoritiesList;
    }

    public void setAuthoritiesList(List<Long> authoritiesList) {
        this.authoritiesList = authoritiesList;
    }

    public List<String> getProvinces() {
        return provinces;
    }

    public void setProvinces(List<String> provinces) {
        this.provinces = provinces;
    }

    public List<LabelValue> getBrands() {
        return brands;
    }

    public void setBrands(List<LabelValue> brands) {
        this.brands = brands;
    }

    public Boolean getAdmin() {
        return isAdmin;
    }

    public void setAdmin(Boolean admin) {
        isAdmin = admin;
    }

    //    public List<LabelValue> getTechnologyDepartment() {
//        return technologyDepartment;
//    }
//
//    public void setTechnologyDepartment(List<LabelValue> technologyDepartment) {
//        this.technologyDepartment = technologyDepartment;
//    }
}
